from flask import Flask, url_for
from flask_cors import *
# import flask.scaffold
# flask.helpers._endpoint_from_view_func = flask.scaffold._endpoint_from_view_func
from flask_restplus import Api
from pathlib import Path
from flask_httpauth import HTTPBasicAuth
from flask_bootstrap import Bootstrap
from blue_prints.file_manager import view_files,ns as vf_api
from blue_prints.insects_api import insects_api,ns
from blue_prints.record import record,lb

# 配置
app = Flask(__name__)
app.register_blueprint(view_files, url_prefix="/view_files")
app.register_blueprint(insects_api, url_prefix="/insects_api")
app.register_blueprint(record,url_prefix="/record")
app.secret_key = 'inetlab-insects-detection'
app.config['JSON_AS_ASCII'] = False
app.config['REFRESH_MSEC'] = 1000
auth = HTTPBasicAuth()
bootstrap = Bootstrap(app)
CORS(app, supports_credentials=True)

api=Api(app, version='1.0', title='insects detection API',
          description='温室病虫害监测接口文档，江苏大学iNetLab团队提供运维')
api.add_namespace(ns)
api.add_namespace(vf_api)
api.add_namespace(lb)
#
# @app.template_filter()
# def file_filter(filefullname, file_name_part):
#     if file_name_part == 1:
#         return str(Path(filefullname).parent)
#     if file_name_part == 2:
#         return str(Path(filefullname).name)
#
# @app.context_processor
# def dir_processor():
#     def format_logs_dir_to_multi(logs_dir):
#         parent_dir_list = list()
#         pa = Path(f'/{logs_dir}')
#         while True:
#             parent_dir_list.append(
#                 {'url': url_for('file_views.index', logs_dir=pa.as_posix()[1:]), 'dir_name': pa.name[:]})
#             pa = pa.parent
#             if pa == Path('/'):
#                 parent_dir_list.append({'url': url_for('file_views.index', logs_dir=''), 'dir_name': '根目录'})
#                 break
#         return parent_dir_list
#
#     return dict(format_logs_dir_to_multi=format_logs_dir_to_multi)
#
# @auth.verify_password
# def verify_password(username, password):
#     if username == 'user' and password == 'mtfy123':
#         return True
#     return False

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000,debug=True)
